package com.party.core.service.notify.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.notify.MemberSetReadDao;
import com.party.core.dao.write.notify.MemberSetWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.YesNoStatus;
import com.party.core.model.notify.MemberSet;
import com.party.core.service.notify.IMemberSetService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 用户消息设置接口实现
 *
 * @author yifeng
 * @date 2018/6/27 0027
 * @time 下午 18:18
 */
@Service
public class MemberSetService implements IMemberSetService {
    @Autowired
    private MemberSetReadDao memberSetReadDao;
    @Autowired
    private MemberSetWriteDao memberSetWriteDao;

    @Override
    public String insert(MemberSet memberSet) {
        BaseModel.preInsert(memberSet);
        boolean result = memberSetWriteDao.insert(memberSet);
        if (result) {
            return memberSet.getId();
        }
        return null;
    }

    @Override
    public boolean update(MemberSet memberSet) {
        memberSet.setUpdateDate(new Date());
        return memberSetWriteDao.update(memberSet);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberSetWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return memberSetWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberSetWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return memberSetWriteDao.batchDelete(ids);
    }

    @Override
    public MemberSet get(String id) {
        return memberSetReadDao.get(id);
    }

    @Override
    public List<MemberSet> listPage(MemberSet memberSet, Page page) {
        return memberSetReadDao.listPage(memberSet, page);
    }

    @Override
    public List<MemberSet> list(MemberSet memberSet) {
        return memberSetReadDao.listPage(memberSet, null);
    }

    @Override
    public List<MemberSet> batchList(Set<String> ids, MemberSet memberSet, Page page) {
        return memberSetReadDao.batchList(ids, new HashedMap(), page);
    }

    @Override
    public MemberSet findByMemberId(String memberId) {
        return memberSetReadDao.findByMemberId(memberId);
    }

    @Override
    public MemberSet initMemberSet(String memberId) {
        MemberSet ms = this.findByMemberId(memberId);
        if (ms == null) {
            MemberSet memberSet = new MemberSet();
            memberSet.setActivitySwitch(YesNoStatus.YES.getCode());
            memberSet.setCrowdfundApplySwitch(YesNoStatus.YES.getCode());
            memberSet.setCrowdfundSuccessSwitch(YesNoStatus.YES.getCode());
            memberSet.setImportantNotifySwitch(YesNoStatus.YES.getCode());
            memberSet.setMemberId(memberId);
            this.insert(memberSet);
            return memberSet;
        }
        return ms;
    }
}
